from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog, QWidget

class MyWindow(QWidget):
    def __init__(self):
        # super().__init__()
        super(MyWindow, self).__init__()
        self.myButton = QtWidgets.QPushButton(self)
        self.myButton.setObjectName("btn")
        self.myButton.setText("保存文本文件")
        self.myButton.clicked.connect(self.save_txt)
        self.txt = 'Hello, World!'

    def save_txt(self):
        txt = self.txt
        filepath, _ = QFileDialog.getSaveFileName(self, "文件保存", "/", '文本文件 (*.json)')
        if filepath:
            with open(filepath, 'w') as file:
                file.write(txt)
                print(f"文件已保存到: {filepath}")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    myshow = MyWindow()
    myshow.show()
    sys.exit(app.exec_())